import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_baselib/lib.dart';
import 'package:flutter_baselib/src/config/bugly_config.dart';
import 'package:flutter_bugly_plugin/lib.dart';

import 'config/log_config.dart';
import 'config/toast_config.dart';

///@date:  2021/2/23 13:52
///@author:  lixu
///@description: 基础库
class BaseLibPlugin {
  ///是否是debug模式
  static bool _isDebug;

  ///全局资源配置类
  static IResConfig _resConfig;

  static bool get isDebug => _isDebug;

  static IResConfig get resConfig => _resConfig;

  ///全局异常捕获，用于bugly异常上报，app main方法中调用
  ///如果不使用该方法，可以自行捕获异常后，调用[FlutterBugly]#[postCatchException]方法手动上报异常到bugly
  ///[callback]
  ///[handler] 异常捕捉后，用于自定义异常处理
  ///[onUploadCallback] 异常捕捉后，上传到bugly时回调该方法
  ///[filterRegExp] 异常上报过滤正则，针对异常message
  ///[debugUpload] debug模式 是否上传错误日志到bugly
  static void postCatchException<T>(
    T callback(), {
    FlutterExceptionHandler handler,
    Function(String errMsg, String errDetail) onUploadCallback,
    String filterRegExp,
    bool debugUpload = true,
  }) {
    FlutterBugly.postCatchException(
      callback,
      onUploadCallback: onUploadCallback,
      handler: handler,
      filterRegExp: filterRegExp,
      debugUpload: debugUpload,
    );
  }

  ///[OneContext] 全局context，用于在没有context的情况下显示dialog，跳转页面...
  static final OneContext oneContext = OneContext();
  static final BuildContext context = oneContext.context;

  ///获取[OneContext]#[builder]
  ///在应用[MaterialApp]#[builder]使用该字段
  static final TransitionBuilder oneContextBuilder = oneContext.builder;

  ///获取[OneContext]#[key]
  ///在应用[MaterialApp]#[navigatorKey]使用该字段
  static final GlobalKey<NavigatorState> oneContextKey = oneContext.key;

  ///初始化基础库,应用启动时调用
  ///[isDebug] 是否是debug模式
  ///[logConfig] 日志库配置
  ///[httpConfig] http相关配置信息
  ///[resConfig] 全局通用资源配置
  ///[buglyConfig] bugly相关配置信息
  ///[toastConfig] Toast相关配置
  static Future<void> init({
    @required bool isDebug,
    @required LogConfig logConfig,
    @required IHttpConfig httpConfig,
    @required IResConfig resConfig,
    BuglyConfig buglyConfig,
    ToastConfig toastConfig,
  }) async {
    assert(isDebug != null && logConfig != null && httpConfig != null && resConfig != null);
    _isDebug = isDebug;
    _resConfig = resConfig;

    ///初始化bugly
    if (buglyConfig != null) {
      await FlutterBugly.init(
        androidAppId: buglyConfig.androidAppId,
        iOSAppId: buglyConfig.iOSAppId,
        channel: buglyConfig.channel,
        autoCheckUpgrade: buglyConfig.autoCheckUpgrade,
        autoInit: buglyConfig.autoInit,
        autoDownloadOnWifi: buglyConfig.autoDownloadOnWifi,
        enableHotfix: buglyConfig.enableHotfix,
        enableNotification: buglyConfig.enableNotification,
        showInterruptedStrategy: buglyConfig.showInterruptedStrategy,
        canShowApkInfo: buglyConfig.canShowApkInfo,
        initDelay: buglyConfig.initDelay,
        upgradeCheckPeriod: buglyConfig.upgradeCheckPeriod,
        checkUpgradeCount: buglyConfig.checkUpgradeCount,
        customUpgrade: buglyConfig.customUpgrade,
      );
    }

    ///初始化日志库
    await LogUtils.init(logConfig);

    ///初始化Toast
    ToastUtils.init(toastConfig);

    ///初始化http组件
    XApi.init(httpConfig);
  }

  static Future<void> dispose() async {
    LogUtils.dispose();
    FlutterBugly.dispose();
  }
}
